package com.darwin.uuas.config;

import com.darwin.uuas.constant.HttpStatus;
import com.darwin.uuas.vo.AjaxResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@Component
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException {
        Object principal = auth.getPrincipal();
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        AjaxResult ajaxResult = new AjaxResult(HttpStatus.SUCCESS,"登陆成功",principal);

    /*    response.setStatus(200);
        Map<String, Object> map = new HashMap<>(16);
        map.put("status",200);
        map.put("msg",principal);*/
        ObjectMapper om = new ObjectMapper();
        out.write(om.writeValueAsString(ajaxResult));
        out.flush();
        out.close();
    }
}
